package org.cangku.controller;

import org.cangku.dao.*;
import org.cangku.entity.*;
import org.cangku.service.PJShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/xiaoshou")
public class XiaoShouInfoController {
    @Autowired
    private PJShopInfoService pjShopInfoService; //商品信息查询
    @Autowired
    private XsLinShiDao xsLinShiDao; //临时表
    @Autowired
    private XiaoShouInfoDao xiaoShouInfoDao;//销售信息详情表
    @Autowired
    private MemberDao memberDao;//会员dao
    @Autowired
    private XiaoShouDao xiaoShouDao; //销售信息表
    @Autowired
    private ShopInfoDao shopInfoDao; //商品信息dao
    @Autowired
    private MemberTypeDao memberTypeDao; //会员类型
    @Autowired
    private KuCunDao kuCunDao;
    @Autowired
    private TiChengDao tiChengDao;
    @Autowired
    private ShopSaleDao shopSaleDao;

    @RequestMapping("insert")
    @ResponseBody
    public Map<String,Object> insert(double yingshou, double youhui, double zhehou, double shishou, int staffid, String phone, String beizhu, String dingdan){
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println("进来了");
        /*产生订单号*/
        String dnumbers = getdingdan();
        /*根据手机号获得会员id*/
        int hyid = memberDao.getidByPhone(phone);
        /*根据单号获得该单号的总销售数量*/
        int znum = xsLinShiDao.getSumCountByDanHao(dingdan);
        /*添加销售信息*/
        Xiaoshou xs = new Xiaoshou();
        xs.setDnumbers(dnumbers);
        xs.setDhyid(hyid);
        xs.setDphone(phone);
        xs.setDnum(znum);
        xs.setDyingshou(yingshou);
        xs.setDzhehou(zhehou);
        xs.setDyouhui(youhui);
        xs.setDshishou(shishou);
        xs.setDstaff(staffid);
        xs.setBeizhu(beizhu);

        int i =xiaoShouDao.insert(xs);
        if(i>0){
            int cz = tiChengDao.cunzai(staffid);
            Ticheng tc = new Ticheng();
            tc.setStaffid(staffid);
            double tcmoney =zhehou*0.01;
            tcmoney = (double) Math.round(tcmoney * 100) / 100;
            /*System.out.println(d);*/
            tc.setTcMoney(tcmoney);
            if(cz>0){
                tiChengDao.update(tc);
            }else{
                tiChengDao.insert(tc);
            }
            /*根据上面获得的会员id给改的会员积分*/
            int jifen = (new Double(zhehou * 0.1)).intValue();
            /*根据会员id增加积分和消费金额*/
            memberDao.updateJiFenAndxMoney(hyid,jifen,zhehou);
            /*根据积分分配会员类型   得到该会员的积分*/
            int getjifen = memberDao.getJiFneById(hyid);
            /*根据得到的积分匹配一下这个积分的最高会员类型*/
            int typeid = memberTypeDao.getMemberTypeId(getjifen);
            /*再修改该会员的会员类型*/
            memberDao.updateMemberTypeid(hyid,typeid);
            map.put("chengbai",1);

            //获得根据单号获取临时表的数据
            List<Xslinshi> linlist = xsLinShiDao.getinfoByDanHao(dingdan);
            /*循环linlist  并把临时表中的数据放到销售详情表中*/
            for (Xslinshi ll : linlist){
                Xiaoshouinfo info = new Xiaoshouinfo();
                info.setDanhao(dnumbers);
                info.setDspid(ll.getXssid());
                info.setDspname(ll.getXsname());
                info.setDchima(ll.getXssize());
                info.setDpinpai(ll.getXspinp());
                info.setDyanse(ll.getXsyanse());
                info.setDfenlei(ll.getXsfenlei());
                info.setDdjmoeny(ll.getXsdj());
                info.setDxsnum(ll.getXsnum());
                info.setDzjwmoeny(ll.getXszongjia());
                /*增加销售信息详情表*/
                xiaoShouInfoDao.xsInfoInsert(info);
                /*添加销售记录表信息*/
                Shopsale ss = new Shopsale();
                System.out.println("商品编号："+info.getDspid());
                ss.setShopId(info.getDspid());
                ss.setMemberphone(phone);
                ss.setShopName(info.getDspname());
                ss.setShopsize(info.getDchima());
                ss.setShopbrand(info.getDpinpai());
                ss.setShopcolor(info.getDyanse());
                ss.setShopfenlei(info.getDfenlei());
                ss.setShopNum(info.getDxsnum());
                ss.setShopPrice(info.getDdjmoeny());
                ss.setShopSum(info.getDzjwmoeny());
                shopSaleDao.insert(ss);
                /*减少库存吧*/
                Kucun kc= new Kucun();
                kc.setSid(ll.getXssid());
                kc.setSname(ll.getXsname());
                kc.setSsize(ll.getXssize());
                kc.setScolor(ll.getXsyanse());
                kc.setStype(ll.getXsfenlei());
                kc.setSbrand(ll.getXspinp());
                kc.setStockNum(ll.getXsnum());
                int num = kuCunDao.getnum(kc);
                double chengben = kuCunDao.getjinjie(kc);
                kc.setCostMoney(num*chengben);

                kuCunDao.updatekucun(kc);
                /*增加玩销售记录和减少完库存删除该订单：dingdan的临时表信息*/
               /* linShiDao.deleteByDanhao(dingdan);*/
            }
        }else{
            map.put("chengbai",2);
        }
        map.put("rows",kuCunDao.queryByTJ("",0,0,0,8));
        map.put("total",kuCunDao.getCountByTJ("",0,0));
        return map;
    }

    @RequestMapping("/getInfoBydanhao")
    @ResponseBody
    public List<Xiaoshouinfo> query(String danhao){
        return xiaoShouInfoDao.query(danhao);
    }

    /*全部退货   把该订单的所有商品退还*/
    @RequestMapping("/tuiall")
    @ResponseBody
    public Map<String,Object> tuiAll(String dingdan){
        Map<String,Object> map = new HashMap<String, Object>();
        /*现根据单号获得该单号的所有信息*/
        List<Xiaoshouinfo> xsinfolist = xiaoShouInfoDao.query(dingdan);
        for (Xiaoshouinfo info : xsinfolist){
            Kucun kc = new Kucun();
            kc.setSid(info.getDspid());
            kc.setSname(info.getDspname());
            kc.setSsize(info.getDchima());
            kc.setSbrand(info.getDpinpai());
            kc.setScolor(info.getDyanse());
            kc.setStype(info.getDfenlei());
            kc.setGoldMoney(info.getDdjmoeny());
            kc.setStockNum(info.getDxsnum());

            int i =kuCunDao.updateAddKucun(kc);
            System.out.println("i受影响行数："+i);
            if(i>0) {
                int num = kuCunDao.getnum(kc);
                double jinjia = kuCunDao.getjinjie(kc);
                kc.setCostMoney(num*jinjia);
                kuCunDao.updatecostMoney(kc);
                int staffid = xiaoShouDao.getStaffid(dingdan);
                Ticheng tc = new Ticheng();
                tc.setStaffid(staffid);
                double zongqian = xiaoShouDao.getZongQian(dingdan);
                double tcmoney =zongqian*0.01;

                BigDecimal b = new BigDecimal(tcmoney);
                tcmoney = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                System.out.println("提成："+tcmoney);
                tc.setTcMoney(tcmoney);
                tiChengDao.updatejine(tc);
                map.put("tui", true);
            }
        }
        xiaoShouInfoDao.delete(dingdan);
        xiaoShouDao.delete(dingdan);

        map.put("rows",xiaoShouDao.query("",0,8));
        map.put("total",xiaoShouDao.getQueryCount(""));

        map.put("xsinfolist",xiaoShouInfoDao.query(""));



        return map;
    }

    @RequestMapping("/dantui")
    @ResponseBody
    public Map<String,Object> dantui(int id){
        Map<String,Object> map = new HashMap<String, Object>();
        Xiaoshouinfo info = xiaoShouInfoDao.getInfoByddinfoid(id);

        Kucun kc = new Kucun();
        kc.setSid(info.getDspid());
        kc.setSname(info.getDspname());
        kc.setSsize(info.getDchima());
        kc.setSbrand(info.getDpinpai());
        kc.setScolor(info.getDyanse());
        kc.setStype(info.getDfenlei());
        kc.setGoldMoney(info.getDdjmoeny());
        kc.setStockNum(info.getDxsnum());
        int i =kuCunDao.updateAddKucun(kc);
        System.out.println("i受影响行数："+i);
        if(i>0) {
            int num = kuCunDao.getnum(kc);
            double jinjia = kuCunDao.getjinjie(kc);
            kc.setCostMoney(num*jinjia);
            kuCunDao.updatecostMoney(kc);
            int staffid = xiaoShouDao.getStaffid(info.getDanhao());
            Ticheng tc = new Ticheng();
            tc.setStaffid(staffid);
            double zongqian = info.getDzjwmoeny();
            double tcmoney =zongqian*0.01;

            BigDecimal b = new BigDecimal(tcmoney);
            tcmoney = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            //System.out.println("提成："+tcmoney);
            tc.setTcMoney(tcmoney);
            tiChengDao.updatejine(tc);
            map.put("tui", true);
        }
        xiaoShouInfoDao.deleteByid(id);
       int fhcount = xiaoShouInfoDao.cunzaidanhao(info.getDanhao());
        //System.out.println("几行："+fhcount);
         if(fhcount>0){

        }else{
            xiaoShouDao.delete(info.getDanhao());
        }

        map.put("rows",xiaoShouDao.query("",0,8));
        map.put("total",xiaoShouDao.getQueryCount(""));

        map.put("xsinfolist",xiaoShouInfoDao.query(info.getDanhao()));

        return map;
    }


    /*生成订单号*/
    public static String getdingdan() {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }

        return  machineId+ String.format("%015d", hashCodeV);
    }
}
